/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月10日         luwei
 */

package com.coocaa.app.gamecenter.framework.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.coocaa.x.uipackage.widget.SlideFocusView.FocusViewRevision;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyListAppData;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class LoadMoreGridView extends GridView {
    private boolean isLoading = false;
    private int mTotalNum = 200;
    private int mHasLoadPage = 0;
    private Context mContext = null;
    private View mLastView = null;
    private LoadMoreDataCallback mLoadMoreDataCallback = null;
    private OnGridItemClickListener mGridItemClickListener = null;
    private SlideFocusView.FocusChangedEvent mFocusChangedEvent = null;
    private SlideFocusView.ItemSelectedEvent mItemSelectedEvent = null;
    private FocusViewRevision mViewRevision = null;
    private final static int SCROLL_ITEM_TIME = 1500;
    private int eventCount = 0;
    private final static int DOUBLE_ROW = 2; //"2"在双数行
    private final static int SINGLE_ROW = 1; //"1"在单行

    public interface LoadMoreDataCallback {
        public List<SkyListAppData> loadMordData(int page);
    }

    public interface OnGridItemClickListener {
        public void onClick(int position, Object baseData);
    }

    public LoadMoreGridView(Context context) {
        super(context);
        this.mContext = context;
        setOnItemClickListener(mItemClickListener);
        setOnItemSelectedListener(mItemSelectedListener);
        setOnFocusChangeListener(mFocusChangeListener);
        setThumbV(R.drawable.game_center_progress_bar);
    }

    public void setFocusChangedEvent(SlideFocusView.FocusChangedEvent focusChangedEvent, FocusViewRevision revision) {
        this.mFocusChangedEvent = focusChangedEvent;
        this.mViewRevision = revision;
    }

    public void setItemSelectedEvent(SlideFocusView.ItemSelectedEvent event, FocusViewRevision revision) {
        this.mItemSelectedEvent = event;
        this.mViewRevision = revision;
    }

    public void setGridItemClickListener(OnGridItemClickListener listener) {
        this.mGridItemClickListener = listener;
    }

    public void setTotalNum(int num) {
        this.mTotalNum = num;
    }

    public void setAnimation(int id) {
        AnimationSet set = new AnimationSet(true);
        set.addAnimation(AnimationUtils.loadAnimation(mContext, id));
        LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        setLayoutAnimation(controller);
    }

    public void setLoadMoreDataCallback(LoadMoreDataCallback callback) {
        this.mLoadMoreDataCallback = callback;
    }

    public void loadData() {
    }

    public void setThumbV(int id) {
        try {
            Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
            mScrollCacheField.setAccessible(true);
            Object mScrollCache = mScrollCacheField.get(this); // scr is your Scroll View

            Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
            scrollBarField.setAccessible(true);
            Object scrollBar = scrollBarField.get(mScrollCache);

            Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
            method.setAccessible(true);
            // Set your drawable here.
            method.invoke(scrollBar, getResources().getDrawable(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private OnItemSelectedListener mItemSelectedListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            LogUtils.i("luwei", "the select position is " + position);
            mLastView = view;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
            LogUtils.i("luwei", "onNothingSelected ");
        }
    };


    private OnFocusChangeListener mFocusChangeListener = new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            LogUtils.i("luwei", "gridview is focus = " + hasFocus);

        }
    };

    private OnItemClickListener mItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (mGridItemClickListener != null) {
                mGridItemClickListener.onClick(position, mAppBaseDatas.get(position));
            }
        }
    };


//    @Override
//    public boolean dispatchKeyEvent(KeyEvent event) {
//        
//        int height = this.getChildAt(1).getHeight() + 60;
//        eventCount ++;
//        //该eventCount%2 是为了取的按键的第一次,因为对不同的item,它会执行两次
//        if(eventCount % 2 != 0 ){
//            int row = 0;
//            row = getItemCurrentRow();
//            if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN && row == DOUBLE_ROW){
//                this.smoothScrollBy(height, SCROLL_ITEM_TIME);
//                LogUtils.d("","向下..滑动执行了");
//            }else if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP && row == SINGLE_ROW){
//                this.smoothScrollBy(-height, SCROLL_ITEM_TIME);
//                LogUtils.d("","向上..滑动执行了");
//            }        
//        }
//        return super.dispatchKeyEvent(event);
//    }
//    
//    /**
//     * 
//     * 获取Gridview中当前item所在的行
//     * @return
//     */
//    public int getItemCurrentRow(){
//        int row = 0;
//        int position = 0;
//        position = this.getSelectedItemPosition();
//        LogUtils.d("","dispatchKeyEvent..position = " + position);
//        LogUtils.d("", "this.getNumColumns() = " + this.getNumColumns());
//        int temp = (position/this.getNumColumns() + 1) % 2; 
//        if(temp == 0){
//            row  = DOUBLE_ROW;
//            LogUtils.d("","在双数行");
//        }else{
//            row = SINGLE_ROW;
//            LogUtils.d("","在单行");
//        }
//        return row;
//    }


    private List<Object> mAppBaseDatas = new ArrayList<Object>();

    public void addMoreDatas(List<Object> datas) {
        isLoading = false;
        if (datas != null) {
            this.mAppBaseDatas.addAll(datas);
        }
    }

    private class GridAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mAppBaseDatas == null ? 0 : mAppBaseDatas.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return convertView;
        }
    }

}
